﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace OA.Utility
{
   public class ToExecl
    {
       /// <summary>
       /// 
       /// </summary>
       /// <param name="ds"></param>
       /// <param name="strhead"></param>
       /// <returns></returns>
       public static string ExportTable(DataTable  dt)
       {
           StringBuilder sb = new StringBuilder();
           int count = 0;

               sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
               sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"10\" rules=\"all\" border=\"1\">");
               ////写出列名，样式随便定义
               sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
               sb.AppendLine("<td style=\"width:100px;\">员工编号</td>");
               sb.AppendLine("<td style=\"width:100px;\">员工姓名</td>");
               sb.AppendLine("<td style=\"width:100px;\">性别</td>");
               sb.AppendLine("<td style=\"width:100px;\">生日</td>");
               sb.AppendLine("<td style=\"width:100px;\">年龄</td>");
               //foreach (DataColumn column in tb.Columns)
               //{
               //    if (column.ColumnName == "标题" || column.ColumnName == "链接")
               //        sb.AppendLine("<td style=\"width:500px;\">" + column.ColumnName + "</td>");
               //    else
               //        sb.AppendLine("<td>" + column.ColumnName + "</td>");
               //}
               sb.AppendLine("</tr>");

               //写出数据
               foreach (DataRow row in dt.Rows)
               {
                   sb.Append("<tr>");
                   //foreach (DataColumn column in tb.Columns)
                   //{
                   //    sb.Append("<td>" + row[column].ToString() + "</td>");
                   //}
                   sb.Append("<td>" + row["EmployeeCardID"].ToString() + "</td>");
                   sb.Append("<td>" + row["EmployeeName"].ToString() + "</td>");
                   sb.Append("<td>" + row["Sex"].ToString() + "</td>");
                   sb.Append("<td  style=\"vnd.ms-excel.numberformat:yyyy/mm/dd\">" + row["Birthday"].ToString() + "</td>");
                   sb.Append("<td>" + row["Age"].ToString() + "</td>");
                   sb.AppendLine("</tr>");
                   count++;
               }
               sb.AppendLine("</table>");

           return sb.ToString();
       }
    }
}
